﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TrainingsApp.WebModel;
using TrainingsApp.Model;
using TrainingsApp.Logic;

namespace TrainingsApp.IPADApp.UserControls2.Teacher {
    public partial class UC_Preview_QuickQuery_Detail : AccountControl {

        protected string Question { get; set; }

        protected int ClassCount { get; set; }

        protected long ClassId {
            get {

                return _classId;
            }
            set { _classId = value; }

        }

        long _classId;

        public string Parameter { get; set; }
        public Guid PreviewId {
            get {

                return new Guid(ZJFramework10.Utility.QueryStringManager.GetValue<string>("pid", Guid.Empty.ToString()));
            }
        }

        protected bool IsError = false;

        protected void Page_Load(object sender, EventArgs e) {
            _classId = ZJFramework10.Utility.QueryStringManager.GetValue<int>("cid", 0);

            TS_Preview_Entity preView = TrainingsApp.Logic.TS_Preview_Logic.TS_Preview_View(PreviewId);
            if (preView != null) {
                var previewDict = TS_Preview_Logic.TS_Preview_ViewByBatch(preView.Batch.Value).ToDictionary(u => u.ClassId.Value, u => u.PreviewId.Value);
                if (_classId == 0) {
                    _classId = preView.ClassId.Value;
                }

                TS_Question_Entity entity = TS_Question_Logic.TS_Question_ViewByPreviewId(PreviewId);
                if (entity != null) {
                    Question = entity.Question;
                }

                var teachder = TrainingsApp.Logic.TS_User_Teacher_Logic.TS_User_Teacher_ViewAllClass(this.CurPage.CurrentUser.UserID).Select(u => new ClassEntity {
                    Name = u.ClassName,
                    Style = "",
                    CurrentStyle = u.ClassesId.Value == ClassId ? "current" : "",
                    ClassId = u.ClassesId.Value,
                    PreviewId = TryGet(previewDict, u.ClassesId.Value)
                });
                rptClass.DataSource = teachder;
                ClassCount = teachder.Count();
                rptClass.DataBind();


                List<TS_Preview_Result_Entity> resultList = null;
                if (ClassId > 0)
                    resultList = TS_Preview_Result_Logic.TS_Preview_Result_GetTypicalByPreviewIdAndClassId(PreviewId, ClassId);
                else
                    resultList = TS_Preview_Result_Logic.TS_Preview_Result_GetTypicalByPreviewId(PreviewId);
                if (resultList != null && resultList.Count > 0) {
                    foreach (TS_Preview_Result_Entity r in resultList) {
                        r.ReferencePercent = Math.Round((r.GroupCount * 100.0 / (r.GroupTotal == 0 ? 1 : r.GroupTotal)), 2);

                    }
                    resultList.Last().Result = "其他";
                }
                Parameter = string.Join("-", resultList.Select(u => u.ReferencePercent));
                if (resultList.Count <= 0) {
                    rptDataSource.Visible = false;
                }
                else {
                    this.rptDataSource.DataSource = resultList;
                    this.rptDataSource.DataBind();
                }
            }
            else {
                Question = "该班级前概念数据被删除或不存在";
                rptDataSource.Visible = false;
                rptClass.Visible = false;
                IsError = true;
            }
        }

        public Guid TryGet(Dictionary<long, Guid> dict, long classId) {
            Guid r = PreviewId;
            if (dict.TryGetValue(classId, out r)) { return r; }
            return Guid.Empty;

        }
    }


    public class ClassEntity {
        public string Name { get; set; }

        public string Style { get; set; }

        public string CurrentStyle { get; set; }

        public long ClassId { get; set; }

        public Guid PreviewId { get; set; }

    }
}